package com.woniuxy.controller;

import com.woniuxy.entity.RbacManager;
import com.woniuxy.entity.RbacManagerDTO;
import com.woniuxy.entity.ResponceEntity;
import com.woniuxy.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/rbacManager")
public class RbacController {
    @RequestMapping("/comeJson")
    public ModelAndView comeJson(@RequestBody String name,String age){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("msg",name+"---"+age);
        modelAndView.setViewName("/index.jsp");
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping("/comeJson1")
    public ModelAndView comeJson(@RequestBody @Valid RbacManagerDTO rbacManager){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("msg",rbacManager);
        modelAndView.setViewName("/index.jsp");
        return modelAndView;
    }

//    form表单响应
    @RequestMapping("/comeFrom01")
    public ModelAndView comeFrom1(String name){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("msg",name);
        modelAndView.setViewName("show");
        return modelAndView;
    }

    @RequestMapping("/comeFrom02")
    public String comeFrom2(ModelMap modelMap, String name){
        modelMap.addAttribute("msg",name);
        return "show";
    }

    @RequestMapping("/comeBack")
    @ResponseBody
    public RbacManagerDTO comeFrom2(@RequestBody RbacManagerDTO rbacManager){
        return rbacManager;
    }

//spring重定向的写法
    @RequestMapping("/send")
    public String comeFrom2(){
        //也可以重定向到页面
        return "redirect:/index.jsp";
       // return "redirect:/rbacManager/comeFrom02?name=999";
    }

    @RequestMapping("/forward")
    public String forward(){
        return "forward:/rbacManager/comeFrom02?name=sasdas";
    }

    //统一响应体
    @ResponseBody
    @RequestMapping("/comeJson05")
    public ResponceEntity comeJson11(@RequestBody RbacManagerDTO rbacManager){
        ResponceEntity<RbacManagerDTO> rbacManagerDTOResponceEntity = new ResponceEntity<>(200,"成功",rbacManager);
        return rbacManagerDTOResponceEntity;
    }

    @ResponseBody
    @RequestMapping("/comeJson06")
    public User comeJson(@RequestBody @Valid User user){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("msg",user);
        modelAndView.setViewName("show");
        return user;
    }

}
